<?php

namespace common\models;

use common\models\interfaces\IArchiveAddon;
use lhs\Yii2SaveRelationsBehavior\SaveRelationsBehavior;
use lhs\Yii2SaveRelationsBehavior\SaveRelationsTrait;
use Yii;
use common\models\base\Archive as Base;
use yii\db\ActiveQuery;

/**
 * This is the model class for table "{{%archive}}".
 *
 * @property integer $id
 * @property string $arc_type
 * @property string $title
 * @property integer $status
 * @property integer $created_by
 * @property integer $created_at
 * @property integer $updated_by
 * @property integer $updated_at
 */
abstract class Archive extends Base implements IArchiveAddon
{
	use SaveRelationsTrait;

//	const STATUS_DELETED = 0;
//	const STATUS_DRAFT = 10;
//	const STATUS_CHECK_PENDING = 20;
//	const STATUS_SECOND_UPDATE = 30;
//	const STATUS_ACTIVE = 40;
//	const STATUS_NORMAL = 50;


	abstract public function getAddOn();

	public static function instantiate($row)
	{
		return ArchiveFactory::factory($row['arc_type']);

	}

	public static function find()
	{
		return parent::find()->andFilterWhere(['arc_type' => ArchiveFactory::getTypeByClassName(static::className())]);
	}

	public function behaviors()
	{
		return [
			'saveRelate' => [
				'class' => SaveRelationsBehavior::className(),
				'relations' => ['addOn'],
			]
		];
	}

	public function transactions()
	{
		return [
			static::SCENARIO_DEFAULT => self::OP_ALL,
		];
	}

	public function getAttachments()
	{
		return $this->hasMany(Attachment::className(), ['archive_id' => 'id'])->inverseOf('archive');
	}

	public function fields()
	{
		return array_merge(parent::fields(),['addOn','author']);
	}

	public function getAuthor()
	{
		return $this->hasOne(User::className(),['id'=>'author_id']);
	}

//	public function getMaterials()
//	{
//		return $this->hasMany(Material::className(),['id'=>'material_id'])->viaTable('rel_archive_and_material',['archive_id'=>'id']);
//    }

}
